/*
Project:				Perry Intergenerational
Author:					Victor Ronda
Original Date:			January 21, 2022
*/



*---------------------------------------*
*  Computing the Table #3 in the Paper  *
*---------------------------------------*



*** Setting environment and filepaths ***
global klmshare : env klmshare
global klmperry : env klmperry
global emp $klmperry/multigen_revision/Empirics

*---------------------------- Loading Results -----------------------------*

use $emp/Results/estimates_jan2022.dta, clear
append using $emp/Results/perm_pval_jan2022.dta
append using $emp/Results/boot_pval_jan2022


global nvarlists = 9

global vlist1 nsusp00 nspecialedu00 narrested00 ghealth200 ntpreg19 yedu23 femp23 ndivor23
global vlist2 nsusp00 nspecialedu00 narrested00 ghealth200 ntpreg19 yedu23 femp23 ndivor23 
global vlist3 nsusp00 nspecialedu00 narrested00 ghealth200 ntpreg19 yedu23 femp23 ndivor23

global sample1 22
global cs1 "Pooled"
global ps1 "Pooled"

global sample2 12
global cs2 "Male"
global ps2 "Pooled"

global sample3 02
global cs3 "Female"
global ps3 "Pooled"

global sample4 21
global cs4 "Pooled"
global ps4 "Male"

global sample5 11
global cs5 "Male"
global ps5 "Male"

global sample6 01
global cs6 "Female"
global ps6 "Male"

global sample7 20
global cs7 "Pooled"
global ps7 "Female"

global sample8 10
global cs8 "Male"
global ps8 "Female"

global sample9 00
global cs9 "Female"
global ps9 "Female"


local l_vname1_nspecialedu00 "Fraction Never in Special Education"
local l_vname1_nsusp00 "Fraction Never Suspended from School"
local l_vname1_narrested00 "Fraction Never Arrested"
local l_vname1_naddicted00 "Fraction Never Addicted"
local l_vname1_ghealth200 "Fraction in Good Health"
local l_vname1_rhs18 "High School Graduation Rate"
local l_vname1_ntpreg19 "Pregnancy Rate (reversed scale)"
local l_vname1_scol21 "Post-Secondary Education Rate"
local l_vname1_ccol23 "College Graduation Rate"
local l_vname1_yedu23 "Average Years of Education"
local l_vname1_femp23 "Employment Rate"
local l_vname1_cmarried23 "Fraction Currently Married"
local l_vname1_ndivor23 "Fraction Never Divorced"


file open latex_table using "$emp/Tables/table_gender.tex", write replace


foreach dvar in $vlist3 {

local k = reverse(substr(reverse("`dvar'"), 1, 2))

local `dvar'_line1 "`l_vname1_`dvar'' "
local `dvar'_line2 ""

forval l = 1/$nvarlists {
foreach estm in aipwd{
local e_`estm' = `dvar'_`estm'_${sample`l'}[1]
local p_`estm' = `dvar'_`estm'_${sample`l'}[2]
}
foreach stat in aipwd {
local estm "e_`stat'"
if `p_aipwd'<=0.1{
local `dvar'_line1 "``dvar'_line1' & $ \mathbf{`: di %5.3f ``estm'' '} $"
}
else if `p_aipwd' >0.1{
local `dvar'_line1 "``dvar'_line1' & $ `: di %5.3f ``estm'' ' $"
}
local estm2 "p_`stat'"
local `dvar'_line2 "``dvar'_line2' & $ [`: di %5.3f ``estm2'' '] $"
}
}
}

foreach dvar in $vlist3 {
	file write latex_table "``dvar'_line1' \\" _n
	file write latex_table "``dvar'_line2' \\" _n
}
file close latex_table
